package com.zp.business.controller.infusion;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.zp.business.entity.infusion.bo.InfusionItemBo;
import com.zp.business.entity.infusion.bo.InfusionRecordBo;
import com.zp.business.entity.infusion.vo.InfusionRecordVo;
import com.zp.business.service.infusion.IInfusionRecordService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 输液室记录控制器
 * 前端访问路由地址为:/business/infusionRecord
 *
 * @author zhang peng
 * @ date 2025-02-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/infusionRecord")
public class InfusionRecordController extends BaseController {

    private final IInfusionRecordService iInfusionRecordService;

    /**
     * 查询输液室记录列表 (分页)
     */
    @SaCheckPermission("business:infusionRecord:list")
    @GetMapping("/selectPage")
    public TableDataInfo<InfusionRecordVo> selectPage(InfusionRecordBo bo, PageQuery pageQuery) {
        return iInfusionRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询输液室记录列表 (不分页)
     */
    @SaCheckPermission("business:infusionRecord:list")
    @GetMapping("/list")
    public R<List<InfusionRecordVo>> list(InfusionRecordBo bo) {
        List<InfusionRecordVo> list = iInfusionRecordService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出输液室记录列表
     */
    @SaCheckPermission("business:infusionRecord:export")
    @Log(title = "输液室记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InfusionRecordBo bo, HttpServletResponse response) {
        List<InfusionRecordVo> list = iInfusionRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "输液室记录", InfusionRecordVo.class, response);
    }

    /**
     * 获取输液室记录详细信息
     *
     * @param infusionRecordId 主键
     */
    @SaCheckPermission("business:infusionRecord:list")
    @GetMapping("/{infusionRecordId}")
    public R<InfusionRecordVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String infusionRecordId) {
        return R.ok(iInfusionRecordService.queryById(infusionRecordId));
    }

    /**
     * 新增输液室记录
     */
    @SaCheckPermission("business:infusionRecord:add")
    @Log(title = "输液室记录", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InfusionRecordBo bo) {
        return toAjax(iInfusionRecordService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 批量新增输液室记录
     */
    @SaCheckPermission("business:infusionRecord:add")
    @Log(title = "输液室记录", businessType = BusinessType.INSERT)
    @PostMapping("/addInfusionRecordBatch")
    public R<Void> addInfusionRecordBatch(@Validated(AddGroup.class) @RequestBody InfusionItemBo bo) {
        return toAjax(iInfusionRecordService.addInfusionRecordBatch(bo) ? 1 : 0);
    }

    /**
     * 修改输液室记录
     */
    @SaCheckPermission("business:infusionRecord:edit")
    @Log(title = "输液室记录", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InfusionRecordBo bo) {
        return toAjax(iInfusionRecordService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 配液弹出框修改输液室记录
     */
    @SaCheckPermission("business:infusionRecord:edit")
    @Log(title = "输液室记录", businessType = BusinessType.UPDATE)
    @PutMapping("/updateInfusionRecordByDialog")
    public R<Void> updateInfusionRecordByDialog(@Validated(EditGroup.class) @RequestBody InfusionRecordBo bo) {
        return toAjax(iInfusionRecordService.updateInfusionRecordByDialog(bo) ? 1 : 0);
    }

    /**
     * 删除输液室记录
     *
     * @param infusionRecordIds 主键串
     */
    @SaCheckPermission("business:infusionRecord:remove")
    @Log(title = "输液室记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infusionRecordIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] infusionRecordIds) {
        return toAjax(iInfusionRecordService.deleteWithValidByIds(Arrays.asList(infusionRecordIds), true) ? 1 : 0);
    }
}
